Kee.js(GV.alias.placeholder, function () {
    'use strict';
    var Login = function () {
        this.$J_userName = $('#J_userName');
        this.$J_pwd = $('#J_pwd');
        this.$J_authCode = $('#J_authCode');
        this.$J_submit = $('#J_submit');
        this.$J_codeWrap = $('#J_codeWrap');
        this.init();
    };
    Login.prototype = {
        constructor: Login,
        setCookie: function (name, value, expireHours) {
            var exdate = new Date();
            exdate.setTime(exdate.getTime() + expireHours * 60 * 60 * 1000);
            document.cookie = name + "=" + escape(value)
                + ((expireHours == null) ? "" : ";expires=" + exdate.toGMTString());
        },
        getCookie: function (name) {
            if (document.cookie.length > 0) {
                var start = document.cookie.indexOf(name + "=");
                if (start != -1) {
                    start = start + name.length + 1;
                    var end = document.cookie.indexOf(";", start);
                    if (end == -1)
                        end = document.cookie.length;
                    return unescape(document.cookie.substring(start, end));
                }
            }
            return '';
        },
        delCookie: function (name) {
            var expdate = new Date();
            expdate.setTime(expdate.getTime() - 1000);
            this.setCookie(name, "", expdate);
        },

        init: function () {
            var yiban = this.getCookie("yiban"),
                _this = this;
            if (yiban != '' && yiban != 'undefined') {
                this.$J_userName.val(yiban);
                this.$J_pwd.focus();
                //check
                $("#J_loginSafe").attr("checked",'true');
            } else {
                this.$J_userName.focus();
            }

            this.$J_submit.on('click', function () {
                if (!_this.isNull()) {
                    _this.login();
                }
                return false;

            });
            $('#J_userName,#J_pwd,#J_authCode').keydown(function (e) {
                if (e.keyCode == 13) {
                    if (!_this.isNull()) {
                        _this.login();
                    }
                }
            }).on('focusin', function () {
               // $(this).parent('li').addClass('login-input-txt-focus');
                $(".pw_li").addClass("pw_li_crt");
                 $(this).addClass("z-text-box");
                if ($(this).attr("name") == "username") {
                    $(".un-icon").addClass("un-icon-crt");
                } else {
                    $(".pw-icon").addClass("pw-icon-crt");
                }
            }).on('focusout', function () {
               // $(this).parent('li').removeClass('login-input-txt-focus');
                $(".pw_li").removeClass("pw_li_crt");
                 $(".text_box").removeClass("z-text-box");
                $(".un-icon").removeClass("un-icon-crt");
                $(".pw-icon").removeClass("pw-icon-crt");
            });
            this.$J_userName.placeholder().select().trigger('focusin');
            this.$J_pwd.placeholder();
        },
        isNull: function () {
            var uNameVal = this.$J_userName.val(),
                uNamePhd = this.$J_userName.attr('placeholder'),
                pwdVal = this.$J_pwd.val(),
//                pwdPhd = this.$J_pwd.attr('placeholder'),
                codeVal = this.$J_authCode.val();
            if (pwdVal === "" && uNameVal === "") {
                this.warning('请输入用户名、密码！');
                this.$J_userName.focus();
                return true;
            }
            if (uNameVal === "" || uNameVal === uNamePhd ) {
                this.warning('请输入账号！');
                this.$J_userName.focus();
                return true;
            }
            if (uNameVal.length < 3) {
                this.warning('请正确输入账号！');
                this.$J_userName.focus();
                return true;
            }
            if (pwdVal === "" || pwdVal.length < 6) {
                this.warning('请输入密码并且密码大于6位!');
                this.$J_pwd.focus();
                return true;
            }
   //  if (!this.$J_codeWrap.is(':hidden')) {
             //   this.$J_authCode.placeholder();
                /*if (codeVal === '' || codeVal === this.$J_authCode.attr('placeholder') || codeVal.length < 4) {
                    this.warning('请输入验证码！');
                    this.$J_authCode.focus();
                    return true;
                }*/
          // }
           /* return false;*/
        },
        login: function () {
            var sel = $("#J_loginSafe").is(':checked'),
                defaultVal = this.$J_submit.val(),
                _this = this;

            if (sel == true) {
                var yiban = this.$J_userName.val();
                _this.setCookie('yiban', yiban, 24*7);
            } else {
                _this.delCookie('yiban');
            }

            $('#loginForm').find('.login-msg-error').hide();

            _this.$J_submit.addClass("disabled").attr({"value": "登录中...", "disabled": true});
            /* 禁用按钮 */

            $.post('home/check/?_' + new Date().getTime(), $("#loginForm").serialize(), function (ret) {
                try {
                    ret = $.parseJSON(ret, true);
                } catch (e) {
                    _this.warning("服务器繁忙，请稍后重试！");
                    return;
                }

                // 成功，直接跳转到指定地址
                if (ret.success) {
                    window.location.href = ret.data;
                    return;
                }

                // 判断验证码
                var err_code = ret.data.code;
                if (err_code == 'NEED_VCODE') {
                    _this.warning('请输入验证码！');
                    _this.vcode().show();
                    _this.$J_authCode.focus();
                    return;
                }
                if (err_code == 'ERROR_VCODE') {
                    _this.warning('验证码错误，请重新输入！');
                    _this.vcode().error();
                    _this.$J_authCode.val('').focus();
                    return;
                }

                _this.vcode().reload();

                switch (err_code) {
                    case 'USER_NOT_FOUND':
                        _this.warning('此账号不存在，请重新输入！');
                        _this.$J_userName.val('').focus();
                        break;
                    case 'WRONG_PASSWORD':
                        _this.warning('密码错误，请重新输入！');
                        _this.$J_pwd.val('').focus();
                        break;
                    case 'USER_HAVE_BANNED':
                        // 封号
                        var str = '<div class="ui-popmsg txtfrom"><div class="ui-form"><ul><li>您好，您的账号因为[<span>'
                            + ret.data.data
                            + '</span>]已被系统锁定。</li><li>如有疑问，可发送邮件至：<b>kf1@shikee.com</b>或拨打客服热线<b>0771-5665208</b>咨询。</li><li style="text-align: center; padding:10px;"><input type="button" class="btn" value="关闭" onclick="closeArt()"></li></ul></div></div>';
                        Kee.use('artDialog', function () {
                            art.dialog({
                                title: '温馨提示',
                                content: str,
                                drag: false,
                                lock: true,
                                resize: false,
                                width: '540px'
                            });
                        });
                        break;
                    default:
                        _this.warning(ret.data.data);
                        break;
                }
            }).complete(function () {
                _this.$J_submit.removeClass("disabled").prop("disabled", false).attr("value", defaultVal);
            });
        },
        warning: function (msg, isShow) {
            /* 错误提示 */
            /* 提示信息,[是否显示](默认显示) */
            var msg = msg || "", isShow = isShow === false ? isShow : true;
            var error_msg = $(".login-msg-error");
            if (error_msg.length === 0) {
                error_msg = $("<li id='login-msg-error'></li>");
                error_msg.prependTo("#loginForm ul").text(msg);
            } else {
                error_msg.text(msg);
            }

            if (isShow) {
                error_msg.html('<i class="login-msg-error-ico"></i>' + $.trim(msg)).show();
            } else {
                error_msg.hide();
            }
        },
        vcode: function () {
            var $wrap = this.$J_codeWrap,
                $img = $wrap.find('img'),
                $input = $('#codetxt');

            function reload(focus) {
                $wrap.show();
                $img.attr('src', '/home/vcode/?_' + (new Date().getTime()));
                $input.val('');
                focus && $input.focus();
            }

            $img.click(function () {
                reload(true);
            });

            need_vcode && reload();

            return {
                show: function () {
                    need_vcode = true;
                    reload(true);
                },
                error: function (focus) {
                    need_vcode = true;
                    $wrap.show();
                    $img.attr('src') || $img.attr('src', '/home/vcode/?_' + (new Date().getTime()));
                    $input.select();
                },
                reload: function () {
                    if (need_vcode) {
                        reload();
                    }
                }
            }
        }
    };
    new Login();
});








